﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Data;
using BookStore.Bussiness;

namespace BookStore.admin
{
    public partial class Manufacturer : System.Web.UI.Page
    {
        static bool flag = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll() {
            //gvMamu.DataSource = manufacturerService.getAll();
            //gvMamu.DataBind();
        }
        public void ResetControl()
        {
            txtmanufacturerID.Text = "";
            txtmanufacturerName.Text ="";
            txtaddress.Text = "";
            txtemail.Text ="";
            txtfax.Text ="";
            txtdescription.Text ="";
        }

        public void insertManu() {
            manufacturerInfo objManu = new manufacturerInfo();
            objManu.manufacturerName = txtmanufacturerName.Text;
            objManu.address = txtaddress.Text;
            objManu.email = txtemail.Text;
            objManu.fax = txtfax.Text;
            objManu.description = txtdescription.Text;
            bool test = manufacturerService.insertManufacturer(objManu);
            if (test==true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateManu()
        {
            manufacturerInfo objManu = new manufacturerInfo();
            objManu.manufacturerID = Convert.ToInt32(txtmanufacturerID.Text);
            objManu.manufacturerName = txtmanufacturerName.Text;
            objManu.address = txtaddress.Text;
            objManu.email = txtemail.Text;
            objManu.fax = txtfax.Text;
            objManu.description = txtdescription.Text;
            bool test = manufacturerService.updateManufacturer(objManu);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void lbtRefreshB_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void gvMamu_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int manuID = Convert.ToInt32(gvMamu.Rows[e.RowIndex].Cells[0].Text);
            if (manufacturerService.checkManu(manuID)==true)
            {
                Response.Write("<script language='javascript'>alert('product still existed , dont delete');</script>");
                return;
            }
            bool test = manufacturerService.deleteManufacturer(manuID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvMamu_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int manuID = Convert.ToInt32(gvMamu.Rows[e.NewSelectedIndex].Cells[0].Text);
            manufacturerInfo objManu = manufacturerService.getbyID(manuID);
            txtmanufacturerID.Text = objManu.manufacturerID.ToString();
            txtmanufacturerName.Text = objManu.manufacturerName;
            txtaddress.Text = objManu.address;
            txtemail.Text = objManu.email;
            txtfax.Text = objManu.fax;
            txtdescription.Text = objManu.description;
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void lbtUpdateT_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                insertManu();
            }
            else
            {
                updateManu();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void gvMamu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMamu.PageIndex = e.NewPageIndex;
            showAll();
        }


    }
}
